package com.googlecode.grima.service;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.googlecode.grima.base.GenericDao;
import com.googlecode.grima.base.GenericEntityService;
import com.googlecode.grima.base.GenericEntityServiceImpl;
import com.googlecode.grima.base.Page;
import com.googlecode.grima.base.SearchResult;
import com.googlecode.grima.base.SearchTemplate;
import com.googlecode.grima.dao.DescriptiveDao;
import com.googlecode.grima.domain.Descriptive;
import com.googlecode.grima.domain.Partner;

@Service("descriptiveService")
public class DescriptiveService extends GenericEntityServiceImpl<Descriptive, Long> implements GenericEntityService<Descriptive, Long> {

	protected DescriptiveDao descriptiveDao;

	@Autowired
	public void setDescriptiveDao(DescriptiveDao descriptiveDao) {
		this.descriptiveDao = descriptiveDao;
	}

	@Override
	public GenericDao<Descriptive, Long> getDao() {
		return descriptiveDao;
	}

	@Override
	public Descriptive getNew() {
		return new Descriptive();
	}

	@Override
	public Descriptive getNewWithDefaults() {
		return getNew();
	}
	
	@Override
	@Transactional
	public void save(Descriptive descriptive) {
		
		Partner customer = descriptive.getCustomer();
		if ((customer != null) && (customer.getId() == null)){
			descriptive.setCustomer(null);
		}
		
		super.save(descriptive);
	}
	
	public SearchResult<Descriptive, Long> findAll(Page page){
		SearchResult<Descriptive, Long> ret = new SearchResult<Descriptive, Long>();
		
		SearchTemplate searchTemplate = new SearchTemplate();
		
		if (page != null) {
			int pageNumber = page.getPageNumber();
			int pageSize = page.getPageSize();
	    int firstResult = ((pageNumber-1) * pageSize);
	    searchTemplate.setFirstResult(firstResult);
	    searchTemplate.setMaxResults(pageSize);
		}
		
    List<Descriptive> descriptives = find(searchTemplate);
		int count = findCount(searchTemplate);

		ret.setTotal(count);
		ret.setRows(descriptives);
		
		return ret;
	}
	
	public SearchResult<Descriptive, Long> findByParameters(String code, String customer, String collection, String segment, Page page) {
		SearchResult<Descriptive, Long> ret = new SearchResult<Descriptive, Long>();
		
		int pageNumber = page.getPageNumber();
		int pageSize = page.getPageSize();
		
    SearchTemplate searchTemplate = new SearchTemplate();
    int firstResult = ((pageNumber-1) * pageSize);
    searchTemplate.setFirstResult(firstResult);
    searchTemplate.setMaxResults(pageSize);
    
    if (StringUtils.isNotEmpty(code)){
    	searchTemplate.addCriterion(Restrictions.like("code", code));
    }

    if (StringUtils.isNotEmpty(customer)){
    	searchTemplate.addCriterion(Restrictions.like("customer", customer));
    }

    if (StringUtils.isNotEmpty(collection)){
    	searchTemplate.addCriterion(Restrictions.like("collection", "%"+collection+"%"));
    }

    if (StringUtils.isNotEmpty(segment)){
    	searchTemplate.addCriterion(Restrictions.like("segment", "%"+segment+"%"));
    }

		List<Descriptive> descriptives = find(searchTemplate);
		int count = findCount(searchTemplate);

		ret.setTotal(count);
		ret.setRows(descriptives);
		
		return ret;
  }
	
	public void deleteById(Long id){
		deleteByPrimaryKey(id);
	}


}
